/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-10 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-10		Created.
 ****************************************************************************
 **/
package com.me.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * <br/>
 * 上午8:40:11 2012-8-10
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class JTalkClientPluginsInfo extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTalkClientFront parent = null;
	private JPanel jPanelSouth;
	private JButton jButtonOK;
	private JScrollPane jScrollPanePlugInList;
	private JList jListPlugIn;
	private JPanel jPanelMainContent;
	private JLabel jLabelName;
	private JLabel jLabelClass;
	private JLabel jLabelDesc;
	private JLabel jLabelNameDis;
	private JLabel jLabelClassDis;
	private JScrollPane jScrollPaneDesc;
	private JTextArea jTextAreaDesc;
	private JScrollPane jScrollPaneMethods;
	private JList jListMehtod;
	private JLabel jLabelIcon;
	private JCheckBox jCheckBoxShortcut;
	private JTalkPlugInLoader myLoader;

	public JTalkClientPluginsInfo(JTalkClientFront parent) {
		this.parent = parent;
		this.initComponents();
	}

	/**
	 * 初始化组件.
	 */
	private void initComponents() {
		this.setTitle("PlugIn Overview");
		this.setName("JFramePluginsInfo");
		this.setResizable(false);

		jPanelSouth = new JPanel();
		jPanelSouth.setPreferredSize(new Dimension(545, 40));
		jButtonOK = new JButton();
		jButtonOK.setText("OK");
		jButtonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickOK();
			}
		});
		jPanelSouth.add(jButtonOK);
		this.getContentPane().add(jPanelSouth, BorderLayout.SOUTH);

		jScrollPanePlugInList = new JScrollPane();
		jListPlugIn = new JList();
		jListPlugIn.setPreferredSize(new Dimension(150, 100));
		jListPlugIn.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				changePlugins();
			}
		});
		jScrollPanePlugInList.setViewportView(jListPlugIn);
		this.getContentPane().add(jListPlugIn, BorderLayout.WEST);

		jPanelMainContent = new JPanel();
		jPanelMainContent.setLayout(null);
		jPanelMainContent.setPreferredSize(new Dimension(80, 320));
		jLabelName = new JLabel();
		jLabelName.setText("Name:");
		jLabelName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelName.setVerticalAlignment(SwingConstants.TOP);
		jLabelName.setBounds(0, 10, 80, 30);
		jPanelMainContent.add(jLabelName);
		jLabelNameDis = new JLabel();
		jLabelNameDis.setText("");
		jLabelNameDis.setVerticalAlignment(SwingConstants.TOP);
		jLabelNameDis.setBounds(90, 10, 170, 30);
		jPanelMainContent.add(jLabelNameDis);
		jLabelClass = new JLabel();
		jLabelClass.setText("Main-Class:");
		jLabelClass.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelClass.setVerticalAlignment(SwingConstants.TOP);
		jLabelClass.setBounds(0, 40, 80, 30);
		jPanelMainContent.add(jLabelClass);
		jLabelClassDis = new JLabel();
		jLabelClassDis.setText("");
		jLabelClassDis.setVerticalAlignment(SwingConstants.TOP);
		jLabelClassDis.setBounds(90, 40, 170, 30);
		jPanelMainContent.add(jLabelClassDis);
		jLabelDesc = new JLabel();
		jLabelDesc.setText("Description:");
		jLabelDesc.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelDesc.setVerticalAlignment(SwingConstants.TOP);
		jLabelDesc.setBounds(0, 70, 90, 30);
		jPanelMainContent.add(jLabelDesc);
		jScrollPaneDesc = new JScrollPane();
		jScrollPaneDesc.setAutoscrolls(true);
		jScrollPaneDesc.setBounds(100, 70, 280, 50);
		jTextAreaDesc = new JTextArea();
		jTextAreaDesc.setText("");
		jTextAreaDesc.setRows(5);
		jTextAreaDesc.setColumns(20);
		jTextAreaDesc.setLineWrap(true);
		jTextAreaDesc.setEditable(false);
		jTextAreaDesc.setWrapStyleWord(true);
		jScrollPaneDesc.setViewportView(jTextAreaDesc);
		jPanelMainContent.add(jScrollPaneDesc);
		jScrollPaneMethods = new JScrollPane();
		jScrollPaneMethods.setBorder(BorderFactory.createTitledBorder("Methods"));
		jScrollPaneMethods.setBounds(10, 130, 370, 140);
		jListMehtod = new JList();
		jScrollPaneMethods.setViewportView(jListMehtod);
		jPanelMainContent.add(jScrollPaneMethods);
		jLabelIcon = new JLabel();
		jLabelIcon.setText("NO ICON");
		jLabelIcon.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		jLabelIcon.setBounds(270, 10, 60, 50);
		jPanelMainContent.add(jLabelIcon);
		jCheckBoxShortcut = new JCheckBox();
		jCheckBoxShortcut.setText("Create a shortcut for this plugin in the main window.");
		jCheckBoxShortcut.setToolTipText("Create a button in JTalk toolbar.");
		jCheckBoxShortcut.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		jCheckBoxShortcut.setMargin(new Insets(0, 0, 0, 0));
		jCheckBoxShortcut.setBounds(10, 280, 370, 15);
		jCheckBoxShortcut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				generateShortcut();
			}
		});
		jPanelMainContent.add(jCheckBoxShortcut);
		this.getContentPane().add(jPanelMainContent, BorderLayout.CENTER);
		this.pack();
	}

	/**
	 * 在指定的文件夹内获取插件.
	 * 
	 * @param dir
	 *            文件夹名.
	 * @return
	 */
	public boolean updatePluginList(String dir) {
		final String[] itemList;
		if (dir != null) {
			this.myLoader = this.parent.getPluginLoader();
			this.myLoader.initPlugins(dir);
			itemList = this.myLoader.getPluginNames();
		} else {
			this.myLoader = this.parent.getPluginLoader();
			itemList = this.myLoader.getPluginNames();
		}
		if (itemList == null || itemList.length == 0) {
			JOptionPane.showMessageDialog(this, "No plugins found in:\n" + dir, "Scanning directory",
					JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
		jListPlugIn.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 1L;
			String[] plugIns = itemList; 
			
			@Override
			public int getSize() {
				if(plugIns == null){
					return 0;
				}
				return plugIns.length;
			}
			
			@Override
			public Object getElementAt(int index) {
				if(plugIns != null){
					return plugIns[index];
				}else{
					return "---";
				}
			}
		});
		return true;
	}
	
	/**
	 * 切换插件时，显示插件的信息.
	 */
	public void changePlugins(){
		String selectedPlugin = (String)this.jListPlugIn.getSelectedValue();
		JTalkClientPlugInInterface plugin = this.myLoader.getPluginByName(selectedPlugin);
		if(plugin == null){
			return;
		}
		this.jLabelNameDis.setText(selectedPlugin);
		this.jLabelIcon.setText("");
		this.jLabelIcon.setOpaque(true);
		this.jLabelIcon.setIcon(plugin.getIcon());
		this.jLabelClassDis.setText(plugin.getClass().getName());
		this.jTextAreaDesc.setText(plugin.getDescription());
		Method[] methods = plugin.getCommandMethods();
		final String[] methodNames = new String[methods.length];
		for(int i = 0; i < methods.length; i++){
			methodNames[i] = methods[i].getName();
		}
		jListMehtod.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 1L;
			String[] names = methodNames;
			@Override
			public int getSize() {
				return names.length;
			}
			
			@Override
			public Object getElementAt(int index) {
				return names[index];
			}
		});
		if(this.isPluginEmbeded(selectedPlugin)){
			this.jCheckBoxShortcut.setSelected(true);
		}else{
			this.jCheckBoxShortcut.setSelected(false);
		}
	}

	/**
	 * 校验插件是否被嵌入到客户端工具栏中.
	 * @param pluginName 插件名称.
	 * @return
	 */
	private boolean isPluginEmbeded(String pluginName) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * 在客户端主窗口工具栏中生成插件快捷方式.
	 */
	private void generateShortcut(){
		String selectedPlugin = (String)this.jListPlugIn.getSelectedValue();
		if(this.jCheckBoxShortcut.isSelected()){
			this.parent.theClient.addPluginShortcut(selectedPlugin);
		}else{
			this.removeEmbededStatusFor(selectedPlugin);
		}
		this.parent.updatePluginShortcuts();
	}

	/**
	 * 去掉插件的快捷方式.
	 * @param plugin
	 */
	private boolean removeEmbededStatusFor(String plugin) {
		return this.parent.theClient.removePluginShortcut(plugin);
	}
	
	/**
	 * 点击OK按钮.
	 */
	private void clickOK(){
		// String selectedPlugin = (String)this.jListPlugIn.getSelectedValue();
		// JTalkClientPlugInInterface plugin = this.myLoader.getPluginByName(selectedPlugin);
		this.dispose();
	}

}
